<?php
/**
 * @title 3020. 子集中元素的最大数量
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[] $nums
     * @return Integer
     */
    function maximumLength($nums) {
        /**
         * @since 2024-01-28 遍历数组nums，统计数字出现次数
         */
        $countArray = [];
        foreach ($nums as $num){
            if(isset($countArray[$num])){
                $countArray[$num]++;
            } else {
                $countArray[$num] = 1;
            }
        }

        /**
         * @since 2024-01-29 记录访问过的点，防止重复遍历
         */
        $visited = [];

        /**
         * @since 2024-01-28 子集中元素的最大数量
         */
        $maxCount = 0;

        /**
         * @since 2024-01-28 遍历数组countArray
         */
        foreach ($countArray as $num=>$count){
            if(isset($visited[$num])){
                // echo "num={$num}, count={$count}, continue\n";
                continue;
            } else {}

            $c = 0;
            if($num == 1){
                if($count%2==0){
                    $c = $count-1;
                } else {
                    $c = $count;
                }
            } else {
                /**
                 * @since 2024-01-29 num=pow(num,2)，不断向上查找
                 */
                $n = $num;
                while(true){
                    if(isset($countArray[$n])){
                        if(!isset($visited[$n])){
                            // echo "n={$n}, exists, visited not set\n";

                            $visited[$n] = true;
                            $count = $countArray[$n];
                            // echo "num={$n}, count={$count}\n";
                            if($count >= 2){
                                $c += 2;
                                $n = pow($n, 2);
                            } else {
                                $c++;
                                break;
                            }
                        } else {
                            // echo "n={$n}, is set, visited=", $visited[$n] ,"\n";
                            $c += $visited[$n];
                            break;
                        }
                    } else {
                        // echo "n={$n}, not found\n";
                        $c--;
                        break;
                    }
                }
            }
            $visited[$num] = $c;
            $maxCount = max($maxCount, $c);
            // echo "num={$num}, count={$count}, c={$c}\n\n\n";
        }

        /**
         * @return Integer
         */
        return $maxCount;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.29.php
 */
$datas = [
    // [[5,4,1,2,2]], // 3
    [[48841,358801,28561,18974736,4356,221,358801,599,13,4356,66,48841,28561,815730721,13,815730721,18974736,66,169,599,169,221]], // 7
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->maximumLength(...$data);
    echo $xhprof->DumpArgs([$result]);
    // var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
